package com.fly.common.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * Author: D.Yang Email: koyangslash@gmail.com Date: 16/8/31 Time: 下午5:42 Describe: String工具类
 */
public class StringUtil
{
    
    // 定义下划线
    private static final char UNDERLINE = '_';
    
    /**
     * 驼峰转下划线工具
     * 
     * @param param 需要转换的字符串
     * @return 转换好的字符串
     */
    public static String camelToUnderline(String param)
    {
        if (StringUtils.isNotEmpty(param))
        {
            int len = param.length();
            StringBuilder sb = new StringBuilder(len);
            for (int i = 0; i < len; ++i)
            {
                char c = param.charAt(i);
                if (Character.isUpperCase(c))
                {
                    sb.append(UNDERLINE);
                    sb.append(Character.toLowerCase(c));
                }
                else
                {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
        return "";
    }
    
    /**
     * 下划线转驼峰工具
     * 
     * @param param 需要转换的字符串
     * @return 转换好的字符串
     */
    public static String underlineToCamel(String param)
    {
        if (StringUtils.isNotEmpty(param))
        {
            int len = param.length();
            StringBuilder sb = new StringBuilder(len);
            for (int i = 0; i < len; ++i)
            {
                char c = param.charAt(i);
                if (c == 95)
                {
                    ++i;
                    if (i < len)
                    {
                        sb.append(Character.toUpperCase(param.charAt(i)));
                    }
                }
                else
                {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
        return "";
    }
    
    /**
     * 在字符串两周添加''
     * 
     * @param param 字符串
     * @return 处理后的字符串
     */
    public static String addSingleQuotes(String param)
    {
        return "\'" + param + "\'";
    }
}
